<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>账号密码添加</title>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">账号密码添加</div>
                        <div class="layui-card-body">
                            <div id="step_input_username" style="display: block;">
                                <form class="layui-form" action="">
                                    <input type="hidden" name="id" id="wechat_id" value="">
                                    <input type="hidden" name="status" value="1">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">微信号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入账户名" class="layui-input" value="">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">密码</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="passwd" lay-verify="passwd" autocomplete="off" placeholder="请输入密码" class="layui-input" value="">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <a class="layui-btn layui-btn-radius" lay-submit lay-filter="formSubmit">提交</a>
                                            <a class="layui-btn layui-btn-radius layui-btn-primary" href="index.html">取消</a>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use('form', function () {
        var $ = layui.$, form = layui.form;

        form.render();

        form.verify({
            name: function (value) {
                if (value.length < 4 || value.length > 16) {
                    return '请输入用户名，4到16个字符';
                }
            },
            passwd: function (value) {
                if (value.length < 6 || value.length > 16) {
                    return '请输入密码，6到16个字符';
                }
            }
        });

        //提交
        form.on('submit(formSubmit)', function (data) {
            layer.msg('提交中', {
                shadeClose: false
                , icon: 16
                , shade: 0.01
            });
            //请求添加微信号的接口
            service.post('api/v1/wxinfo', {
                name: data.field.name,
                passwd: data.field.passwd
            }).then((response) => {
                if (response.code === 200) {
                    layer.msg('添加成功', {offset: '45vh', icon: 1, time: 2000
                    }, function () {
                        // T跳转到列表页
                        location.href = '../wechat/index.html'
                    });
                } else {
                    layer.msg(response.msg, {offset: '45vh', icon: 5, time: 2000});
                }
            }).catch((err) => {
                layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
            })
            return false
        });
    })
</script>
</body>
</html>
